//
//  ChooseViewController.m
//  庐山通
//
//  Created by zcj on 15-10-23.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "ChooseViewController.h"
#import "MJRefresh.h"
#import "Grouble.h"
#import "SelectFindHttp.h"
#import "MBProgressHUD+MJ.h"
#import "ChooseViewCell.h"
#import "ChooseModelFrame.h"
#import "ChooseModel.h"


#define MyFindCell @"HGFindCellName"

@interface ChooseViewController ()<MJRefreshBaseViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak)UITableView * findTableView;

@property (nonatomic,strong) MJRefreshHeaderView *header;
@property (nonatomic,strong) MJRefreshFooterView *footer;

@property (nonatomic,strong)NSMutableArray *ChooseDataFrame;
@property (nonatomic,strong)NSMutableArray *ChooseTempArray;

@end

@implementation ChooseViewController
- (NSMutableArray *)ChooseDataFrame
{
    if (_ChooseDataFrame == nil) {
        _ChooseDataFrame = [[NSMutableArray alloc]init];
    }
    return _ChooseDataFrame;
}
- (NSMutableArray *)ChooseTempArray
{
    if (_ChooseTempArray == nil) {
        _ChooseDataFrame = [NSMutableArray arrayWithCapacity:100];
    }
    return _ChooseDataFrame;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTableView];
    [self setRush];
    self.view.backgroundColor = [UIColor redColor];
    // Do any additional setup after loading the view.
}
- (void)addTableView
{//findTableView
    // collection的一个布局
    UITableView * findTableView = [[UITableView alloc]init];
    findTableView.frame = self.view.bounds;
    findTableView.delegate = self;
    findTableView.dataSource = self;
    [self.view addSubview:findTableView];
    self.findTableView = findTableView;
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.findTableView;
    head.delegate = self;
    [head beginRefreshing];
    self.header = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.findTableView;
    foot.delegate = self;
    self.footer = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    [SelectFindHttp getWirhUrl:HGURL parms:nil success:^(id json) {
        NSMutableArray *bannerFrame=[NSMutableArray array];
        
        NSArray *tempGoods = json[@"data"][@"goods_list"];
        NSLog(@"%@",tempGoods);
        
        // 获取模型对象
        NSArray *findArrayTemp = [ChooseModel objectArrayWithKeyValuesArray:tempGoods];
        
        for(ChooseModel * findModel in findArrayTemp){
            ChooseModelFrame * findModelFrame = [[ChooseModelFrame alloc]init];
            findModelFrame.chooseModel = findModel;
            [bannerFrame addObject:findModelFrame];
        }
        // 把新获取的frame放入temparray里面去
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        self.ChooseDataFrame = bannerFrame;
        //        NSMutableArray * tempArray = [NSMutableArray array];
        //        [tempArray addObject:bannerFrame];
        //        [tempArray addObject:self.dataFrame];
        //        self.dataFrame = tempArray;
        [self.findTableView reloadData];
        [self.footer endRefreshing];
        
        [MBProgressHUD showSuccess:@"下拉刷新成功"];
    } failture:^(id error) {
        NSLog(@"%@",error);
    }];
}
/***
 1:请求数据成nsarray
 2:转换成模型数据
 3:算出数据的frame
 4:放入临时数组中
 5:把临时数组中的数据负值给数据
 ***/
- (void)loadNewData
{
    [SelectFindHttp getWirhUrl:HGURL parms:nil success:^(id json) {
        NSMutableArray *bannerFrame=[NSMutableArray array];
        
        NSArray *tempGoods = json[@"data"][@"goods_list"];
        NSLog(@"%@",tempGoods);
        
        // 获取模型对象
        NSArray *findArrayTemp = [ChooseModel objectArrayWithKeyValuesArray:tempGoods];
        
        for(ChooseModel * findModel in findArrayTemp){
            ChooseModelFrame * findModelFrame = [[ChooseModelFrame alloc]init];
            findModelFrame.chooseModel = findModel;
            [bannerFrame addObject:findModelFrame];
        }
        // 把新获取的frame放入temparray里面去
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        //self.dataFrame = [bannerFrame addObject:self.dataFrame];
        self.ChooseDataFrame = bannerFrame;
        //        NSMutableArray * tempArray = [NSMutableArray array];
        //        [tempArray addObject:bannerFrame];
        //        [tempArray addObject:self.dataFrame];
        //        self.dataFrame = tempArray;
        [self.findTableView reloadData];
        [self.header endRefreshing];
        
        [MBProgressHUD showSuccess:@"下拉刷新成功"];
    } failture:^(id error) {
        NSLog(@"%@",error);
    }];
}
- (void)dealloc
{
    [self.footer free];
    [self.header free];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ChooseDataFrame.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseViewCell * cell = [ChooseViewCell ChooseViewCell:tableView];
    ChooseModelFrame * MyChooseFrame = self.ChooseDataFrame[indexPath.row];
    cell.chooseModelFrame = MyChooseFrame;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseModelFrame * modelFrame = self.ChooseDataFrame[indexPath.row];

    
    return modelFrame.cellH;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
